package it.lolled.core;

import it.lolled.entity.Post;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * Hello world!
 *
 */
public class AppMemeCenter {
	public static void main(String[] args) {
		new AppMemeCenter().work();
	}

	public void work() {

		Document doc;
		try {

			// need http protocol
			doc = Jsoup.connect("http://www.memecenter.com/").get();

			List<Post> postList = new LinkedList<Post>();
			HttpSend httpSend = new HttpSend();

			// get all links
			Elements postElements = doc.select(".rrcont");
			for (Element postElement : postElements) {
				try {
					String id = postElement.attr("data-id");
					String title = postElement.attr("alt");

					String imageUrl = "";
					imageUrl = postElement.attr("src");

					if (!"".equals(imageUrl) && !imageUrl.contains("unsafe")) {
						postList.add(new Post("memecenter_"+id, title, imageUrl , "memecenter"));
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			for (Post post : postList) {
				System.out.println(post.getInsert());
				httpSend.postInsert(post);
			}

		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}
